﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Entities;
using VipCo.Data;
using System.Data;
using System.Threading;

public partial class ChuyenMuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString["IdChuyenMuc"] != null)
            {
                H_CatalogId.Value = Request.QueryString["IdChuyenMuc"].ToString();
                int TotalRows = 0;
                TList<DiaDiem> TList_TinTuc = DataRepository.DiaDiemProvider.GetPaged("TrangThai = 1 and IdChuyenMuc=" + H_CatalogId.Value, "Id ASC", 0, 0, out TotalRows);
                NhomChuyenMuc entChuyenMuc = DataRepository.NhomChuyenMucProvider.GetById(TList_TinTuc[0].IdChuyenMuc);
                littitle.Text = entChuyenMuc.TenChuyenMuc;
                lblnhom.Text = TotalRows.ToString();
                Pager1.CurrentIndex = 1;
                HienThiNhomDiaDiem(Pager1.CurrentIndex);
            }
        }
    }

    private void HienThiNhomDiaDiem(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "IdChuyenMuc = " + H_CatalogId.Value;
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("DiaDiem_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        //TList<DiaDiem> TList_TinTuc = DataRepository.DiaDiemProvider.GetPaged("TrangThai = 1 and Id=" + H_CatalogId.Value, "Id ASC", 0, 0, out TotalRows);
        dlChuyenmuctt.DataSource = dsTinTuc;
        dlChuyenmuctt.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        Thread.Sleep(2000);
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienThiNhomDiaDiem(currnetPageIndex);
    }


    /// <summary>
    /// Handles ItemDataBound, Binds ExchangesTo to the Child DataList
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dlFromTrades_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            DataListItem parentList = e.Item;
            Literal lit_Name = (Literal)parentList.FindControl("lit_Id");
            string i = (lit_Name.Text).ToString();
            int j = Convert.ToInt32(i);
            int TotalRows = 0;
            TList<DiaDiem> TList_DiaDiems = DataRepository.DiaDiemProvider.GetPaged("IdChuyenMuc = " + j, "Id ASC", 0, 6, out TotalRows);
            // Retrieve the Parent List


            // Find the To Trades DataList Control
            DataList dlToTrades = (DataList)parentList.FindControl("dlChuyenmuctt");

            // Bind the ExchangeTo DataList
            dlToTrades.DataSource = TList_DiaDiems;
            dlToTrades.DataBind();
        }
    }

    public string GetSo(object Char)
    {
        string s = Char.ToString();
        int TotalRows = 0;
        TList<DiaDiem> TList_TinTuc = DataRepository.DiaDiemProvider.GetPaged("IdChuyenMuc = " + s, "Id ASC", 0, 0, out TotalRows);
        return TotalRows.ToString();
    }
}